//
//  TransferApplyViewController.m
//  nonghubao
//
//  Created by Fenrir on 13-5-18.
//  Copyright (c) 2013年 Chongqing Nonghubao Co., Ltd. All rights reserved.
//

#import "TransferApplyViewController.h"
#import "Helper.h"
#import "Config.h"
#import "RegexKitLite.h"
#import "RTLabel.h"
#import "JMFBase.h"
#import <QuartzCore/QuartzCore.h>


//  -业务逻辑细节说明-
//
//  @abstract       本类的业务逻辑较为简单，根据用户填写的数据向服务器发送转账申请的请求，在这之前需要对表单数据进行适当的数据校验，比如金额数的校验。

//  @discussion

//  @notes


//  -类修改日志-
//  修改人   修改时间   针对版本
//  修改描述

//最底部按钮的高度
#define BOTTOM_BUTTON_HEIGHT _SYS_FRAME_HEIGHT - 20.0f - 44.0f - 44.0f

@interface TransferApplyViewController ()

@end

@implementation TransferApplyViewController
@synthesize transferInfo = _transferInfo;
@synthesize balance = _balance;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    
    UIButton *backButton = [Helper buttonWithStyle:ButtonStyleNavigationReturn];
    [backButton addTarget:self action:@selector(backButtonEvent:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem* leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    self.navigationItem.leftBarButtonItem = leftBarButtonItem;
    
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"com_bg_012@2x" ofType:@"png"]]];
    
    _scrollView = [[CustomScrollView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, _SYS_FRAME_HEIGHT)];
    _scrollView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"com_bg_012@2x" ofType:@"png"]]];
    _scrollView.customDelegate = self;
    _scrollView.showsVerticalScrollIndicator = NO;
    _scrollView.showsHorizontalScrollIndicator = NO;
    CGSize newSize = CGSizeMake(self.view.frame.size.width, _SYS_FRAME_HEIGHT + 50.0f);
    
    [_scrollView setContentSize:newSize];
    [self.view addSubview:_scrollView];
    
    //温馨提示view
    UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 90.0f)];
    titleView.backgroundColor = [UIColor whiteColor];
    
    [_scrollView addSubview:titleView];
    
    //温馨提示
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(125.0f, 5.0f, 80.0f, 30.0f)];
    titleLabel.font = _SYS_FONT_005;
    titleLabel.text = _L(@"title_15");
    [titleView addSubview:titleLabel];
    
    //分割线
    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, titleLabel.frame.origin.y + titleLabel.frame.size.height + 5.0f, 320.0f, 0.5f)];
    
    lineView.backgroundColor = [UIColor colorWithRed:219.0f/255.0f green:219.0f/255.0f blue:219.0f/255.0f alpha:1.0f];
    
    [titleView addSubview:lineView];
    
    //提示内容
    RTLabel *contentLabel = [[RTLabel alloc] initWithFrame:CGRectMake(10.0f, lineView.frame.origin.y + lineView.frame.size.height + 15.0f, 300.0f, 25.0f)];
    contentLabel.font = _SYS_FONT_005;
    contentLabel.text = _L(@"title_63");
    [titleView addSubview:contentLabel];
    
    
    //开户银行信息view
    UIView *bankInfoView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, titleView.frame.origin.y
                                                                    + titleView.frame.size.height + 10.0f, 320.0f, 100.f)];
    bankInfoView.backgroundColor = [UIColor whiteColor];
    
    [_scrollView addSubview:bankInfoView];
    
    
    //开户银行名称
    UILabel *bankNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(10.0f, 10.0f, 100.f, 25.0f)];
    bankNameLabel.text = _L(@"title_64");
    bankNameLabel.textAlignment = UITextAlignmentRight;
    bankNameLabel.font = _SYS_FONT_003;
    [bankInfoView addSubview:bankNameLabel];
    
    
    UILabel *bankName = [[UILabel alloc] initWithFrame:CGRectMake(bankNameLabel.frame.origin.x + bankNameLabel.frame.size.width, 10.0f, 200.0f, 25.0f)];
    bankName.text = [_transferInfo objectForKey:@"bankName"];
    bankName.textColor = [UIColor lightGrayColor];
    bankName.font = _SYS_FONT_003;
    [bankInfoView addSubview:bankName];
    
    
    //银行卡号
    UILabel *cardIdLabel = [[UILabel alloc] initWithFrame:CGRectMake(10.0f, bankNameLabel.frame.origin.y
                                                                     + bankNameLabel.frame.size.height, 100.f, 20.0f)];
    cardIdLabel.text = _L(@"title_65");
    cardIdLabel.textAlignment = UITextAlignmentRight;
    cardIdLabel.font = _SYS_FONT_003;
    [bankInfoView addSubview:cardIdLabel];
    
    
    UILabel *cardId = [[UILabel alloc] initWithFrame:CGRectMake(cardIdLabel.frame.origin.x + cardIdLabel.frame.size.width, bankNameLabel.frame.origin.y
                                                                + bankNameLabel.frame.size.height, 200.0f, 20.0f)];
    cardId.text = [_transferInfo objectForKey:@"bankAccount"];
    cardId.textColor = [UIColor lightGrayColor];
    cardId.font = _SYS_FONT_003;
    [bankInfoView addSubview:cardId];
    
    //姓名
    UILabel *realNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(10.0f, cardIdLabel.frame.origin.y
                                                                     + cardIdLabel.frame.size.height, 100.f, 20.0f)];
    realNameLabel.text = _L(@"title_66");
    realNameLabel.textAlignment = UITextAlignmentRight;
    realNameLabel.font = _SYS_FONT_003;
    [bankInfoView addSubview:realNameLabel];
    
    
    UILabel *realName = [[UILabel alloc] initWithFrame:CGRectMake(realNameLabel.frame.origin.x + realNameLabel.frame.size.width, cardId.frame.origin.y
                                                                + cardId.frame.size.height, 200.0f, 20.0f)];
    realName.text = [_transferInfo objectForKey:@"realName"];
    realName.textColor = [UIColor lightGrayColor];
    realName.font = _SYS_FONT_003;
    [bankInfoView addSubview:realName];
    
    //余额
    UILabel *balanceLabel = [[UILabel alloc] initWithFrame:CGRectMake(10.0f, realNameLabel.frame.origin.y
                                                                       + realNameLabel.frame.size.height, 100.f, 20.0f)];
    balanceLabel.text = _L(@"title_68");
    balanceLabel.textAlignment = UITextAlignmentRight;
    balanceLabel.font = _SYS_FONT_003;
    [bankInfoView addSubview:balanceLabel];
    
    
    UILabel *balance = [[UILabel alloc] initWithFrame:CGRectMake(realNameLabel.frame.origin.x + realNameLabel.frame.size.width, realName.frame.origin.y
                                                                  + realName.frame.size.height, 200.0f, 20.0f)];
    balance.text = [NSString stringWithFormat:_L(@"title_amount_04"), _balance];
    balance.textColor = [UIColor lightGrayColor];
    balance.font = _SYS_FONT_003;
    [bankInfoView addSubview:balance];

    
    //转账金额View
    UIView* amountView = [[UIView alloc] initWithFrame:CGRectMake(10, bankInfoView.frame.origin.y + bankInfoView.frame.size.height + 10.0f, 300, 40)];
    amountView.backgroundColor = [UIColor whiteColor];
    amountView.layer.cornerRadius = 3;
    amountView.layer.borderWidth = 1;
    amountView.layer.borderColor = [_SYS_COLOR_003 CGColor];
    [_scrollView addSubview:amountView];
    
    //转账金额Label
    UILabel *amountLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 75, 20)];
    amountLabel.font = _SYS_FONT_003;
    amountLabel.text = _L(@"title_67");
    [amountView addSubview:amountLabel];
    
    //转账金额输入框
    _amountTextField = [[UITextField alloc] initWithFrame:CGRectMake(80, 10, 200, 25)];
    _amountTextField.returnKeyType = UIReturnKeyDone;
    _amountTextField.keyboardType = UIKeyboardTypeDecimalPad;
    _amountTextField.tag = 1;
    _amountTextField.textColor = [UIColor lightGrayColor];
    _amountTextField.delegate = self;
    _amountTextField.placeholder = _L(@"holder_27");
    _amountTextField.font =_SYS_FONT_003;
    [amountView addSubview:_amountTextField];
    
    //安全密码View
    UIView* securityPwdView = [[UIView alloc] initWithFrame:CGRectMake(10, amountView.frame.origin.y + amountView.frame.size.height + 10.0f, 300, 40)];
    securityPwdView.backgroundColor = [UIColor whiteColor];
    securityPwdView.layer.cornerRadius = 3;
    securityPwdView.layer.borderWidth = 1;
    securityPwdView.layer.borderColor = [_SYS_COLOR_003 CGColor];
    [_scrollView addSubview:securityPwdView];
    
    //转账金额Label
    UILabel *securityPwdLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 75, 20)];
    securityPwdLabel.font = _SYS_FONT_003;
    securityPwdLabel.text = _L(@"title_59");
    [securityPwdView addSubview:securityPwdLabel];
    
    //转账金额输入框
    _securityPwdTextField = [[UITextField alloc] initWithFrame:CGRectMake(80, 10, 200, 25)];
    _securityPwdTextField.returnKeyType = UIReturnKeyDone;
    _securityPwdTextField.tag = 2;
    _securityPwdTextField.secureTextEntry = YES;
    _securityPwdTextField.textColor = [UIColor lightGrayColor];
    _securityPwdTextField.delegate = self;
    _securityPwdTextField.font =_SYS_FONT_003;
    _securityPwdTextField.placeholder = _L(@"holder_28");
    [securityPwdView addSubview:_securityPwdTextField];
    
    //按钮集（编写顺序是从下自上）
    
    //中间按钮
    UIButton *middleButton = [[UIButton alloc] initWithFrame:CGRectMake(10.0f, BOTTOM_BUTTON_HEIGHT, 300.0f, 44.0f)];
    [middleButton setBackgroundImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"com_btn_006_n@2x" ofType:@"png"]] forState:UIControlStateNormal];
    [middleButton setTitle:_L(@"btn_transfer_01") forState:UIControlStateNormal];
    [middleButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    middleButton.titleLabel.font = _SYS_FONT_007;
    middleButton.tag = 2;
    [middleButton addTarget:self action:@selector(buttonEvent:) forControlEvents:UIControlEventTouchUpInside];
    
    [_scrollView addSubview:middleButton];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - 自定义事件

- (void)backButtonEvent:(id)sender
{
    [self.navigationController dismissModalViewControllerAnimated:YES];
}

- (void)buttonEvent:(id)sender
{
    
    JSONRequest* request = [[JSONRequest alloc] initWithURLString:_JSON_URL_ACCOUNT_TRANSFER_APPLY];
    request.hudOn = true;
    request.delegate = self;
    request.hudTitle = _L(@"title_loading_13");
    
    if (![_amountTextField.text isMatchedByRegex:@"^\\d+(\\.\\d+)?$"]) {
        [Helper showAlertWithTitle:nil andMessage:_L(@"alert_51")];
        return;
    }
    
    [request setValue:[_transferInfo objectForKey:@"id"] forParameter:@"transferAccountId"];
    [request setValue:_amountTextField.text  forParameter:@"amount"];                       //转账金额
    [request setValue:_securityPwdTextField.text forParameter:@"securityPwd"];              //安全密码
    [request asynchronous];
    
}

#pragma mark - 私有方法

- (void)recoverView
{
    NSTimeInterval animationDuration = 0.30f;
    [UIView beginAnimations:@"ResizeForKeyboard" context:nil];
    [UIView setAnimationDuration:animationDuration];
    CGRect rect = CGRectMake(0.0f, 0.0f, self.view.frame.size.width, self.view.frame.size.height);
    self.view.frame = rect;
    [UIView commitAnimations];
}

#pragma mark - UITextField Delegate

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if (textField.tag == 1) {
        
        if (![Helper testNumberWithString:[textField.text stringByAppendingString:string]]) {
            [Helper showAlertWithTitle:nil andMessage:_L(@"alert_51")];    
            return NO;
        }
    }
    return YES;
}

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    UIView *superView = [textField superview];
    
    float offset = superView.frame.origin.y + superView.frame.size.height - (self.view.frame.size.height - 216);
    NSTimeInterval animationDuration = 0.30f;
    [UIView beginAnimations:@"ResizeForKeyBoard" context:nil];
    [UIView setAnimationDuration:animationDuration];
    
    if(offset > 0)
    {
        _scrollView.contentOffset = CGPointMake(0.0f, offset);
    }
    
    [UIView commitAnimations];
    
    return YES;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [_amountTextField resignFirstResponder];
    [_securityPwdTextField resignFirstResponder];
    return YES;
}

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{    
    return YES;
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [_amountTextField resignFirstResponder];
    [_securityPwdTextField resignFirstResponder];
}

#pragma mark - JSONRequestDelegate

- (void)requestDidLoadJSON:(NSDictionary *)dict
{
    JMFBase* jmf = [[JMFBase alloc] initWithJSONDictionary:dict];
    
    if (jmf.status == JMStatusSucceed) {
        
        [Helper showAlertWithTitle:@"" andMessage:_L(@"alert_50")];
        
        [self.navigationController dismissModalViewControllerAnimated:YES];
    }
    else {
        [Helper easyShowJMFStatus:jmf];
    }
}

//
- (void)requestFailWithNetworkUnavailabe
{
    [Helper showAlertWithTitle:nil andMessage:_L(@"title_networkunavailabe")];
}
@end
